package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

type User struct {
	id                 int
	username, password string
}

var sqldata map[interface{}]interface{}

func main() {
	var u User
	db, err := sql.Open("mysql", "root:root@/test?charset=utf8")
	check(err)
	//插入数据
	stmt, err := db.Prepare("INSERT user SET username=?,password=?")
	check(err)
	res, err := stmt.Exec("chenxinchang", "liuchen")
	check(err)
	id, err := res.LastInsertId()
	check(err)
	fmt.Println(id)
	//查询数据
	rows, err := db.Query("SELECT * FROM user")
	check(err)
	fmt.Println(rows.Columns())
	userinfo := make(map[interface{}]interface{})
	for rows.Next() {
		err := rows.Scan(&u.id, &u.username, &u.password)
		check(err)
		userinfo[u.id] = u
	}
	fmt.Println(userinfo)
}
func check(err error) {
	if err != nil {
		fmt.Println(err)
	}
}
